//
//  XJVisitorView.m
//  weiboOC
//
//  Created by 徐绩 on 16/4/17.
//  Copyright © 2016年 ayumi. All rights reserved.
//

#import "XJVisitorView.h"

#define MAS_SHORTHAND
#define MAS_SHORTHAND_GLOBALS

#import <Masonry/Masonry.h>

@interface XJVisitorView ()

@property (nonatomic,strong) UIImageView *iconImage;
@property (nonatomic,strong) UIImageView *circleImage;
@property (nonatomic,strong) UIImageView *maskImage;
@property (nonatomic,strong) UILabel *messageLabel;
@property (nonatomic,strong) UIButton *registBtn;
@property (nonatomic,strong) UIButton *loginBtn;

@end

@implementation XJVisitorView

-(instancetype)initWithFrame:(CGRect)frame{

    if (self = [super initWithFrame:frame]) {
        
        [self setupUI];
    }
    return self;
}

-(void)setVisitorInfo:(NSString *)title image:(NSString *)imageName{

    if (title.length > 0) {
        
        self.messageLabel.text = title;
        self.iconImage.image = [UIImage imageNamed:imageName];
    }else{
    
        [self setupAnim];
    }
}

#pragma mark - 设置动画
-(void)setupAnim{

    self.circleImage.hidden = NO;
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    anim.duration = 20;
    anim.repeatCount = MAXFLOAT;
    anim.toValue = @(2 * M_PI);
    anim.removedOnCompletion = NO;
    [self.circleImage.layer addAnimation:anim forKey:nil];

}

#pragma mark - 布局界面
-(void)setupUI{

    self.iconImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"visitordiscover_feed_image_house"]];
    self.circleImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"visitordiscover_feed_image_smallicon"]];
    self.maskImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"visitordiscover_feed_mask_smallicon"]];
    self.circleImage.hidden = YES;
    
    self.messageLabel = [[UILabel alloc]init];
    self.messageLabel.textColor = [UIColor darkGrayColor];
    self.messageLabel.font = [UIFont systemFontOfSize:14];
    self.messageLabel.preferredMaxLayoutWidth = 245;
    self.messageLabel.textAlignment = NSTextAlignmentCenter;
    self.messageLabel.text = @"关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜";
    self.messageLabel.numberOfLines = 0;
    
    self.registBtn = [[UIButton alloc]init];
    [self.registBtn setTitle:@"注册" forState:UIControlStateNormal];
    [self.registBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [self.registBtn setBackgroundImage:[UIImage imageNamed:@"common_button_white"] forState:UIControlStateNormal];
    [self.registBtn addTarget:self action:@selector(didClickRegistBtn) forControlEvents:UIControlEventTouchUpInside];
    
    self.loginBtn = [[UIButton alloc]init];
    [self.loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [self.loginBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [self.loginBtn setBackgroundImage:[UIImage imageNamed:@"common_button_white"] forState:UIControlStateNormal];
    [self.loginBtn addTarget:self action:@selector(didClickLoginBtn) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:self.circleImage];
    [self addSubview:self.maskImage];
    [self addSubview:self.iconImage];
    [self addSubview:self.messageLabel];
    [self addSubview:self.registBtn];
    [self addSubview:self.loginBtn];
}

-(void)layoutSubviews{
    [super layoutSubviews];
    [self.iconImage makeConstraints:^(MASConstraintMaker *make) {
        
        make.center.equalTo(self);
    }];
    [self.circleImage makeConstraints:^(MASConstraintMaker *make) {
        
        make.center.equalTo(self.iconImage);
    }];
    [self.maskImage makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.iconImage);
    }];
    
    [self.messageLabel makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.circleImage.bottom).offset(10);
        make.centerX.equalTo(self.iconImage);
    }];
    [self.registBtn makeConstraints:^(MASConstraintMaker *make) {
       
        make.leading.equalTo(self.messageLabel);
        make.top.equalTo(self.messageLabel.bottom).offset(10);
        make.width.offset(100);
    }];
    [self.loginBtn makeConstraints:^(MASConstraintMaker *make) {
        
        make.trailing.equalTo(self.messageLabel);
        make.top.width.equalTo(self.registBtn);
        
    }];
}

#pragma mark - 按钮点击
-(void)didClickRegistBtn{

    if ([self.delegate respondsToSelector:@selector(didClickRegistBtn)]) {
        
        [self.delegate didClickLoginBtn];
    }
}

-(void)didClickLoginBtn{

    if ([self.delegate respondsToSelector:@selector(didClickLoginBtn)]) {
        
        [self.delegate didClickLoginBtn];
    }
}
@end
